using System.Xml.Linq;
using Engine;
using Engine.Graphics;
using Game;

namespace SCIENEW {
    public class CokeOvenWidget : CanvasWidget {
        public GridPanelWidget m_inventoryGrid;

        public GridPanelWidget m_metalGrid;

        public ComponentCokeOven m_componentCraftingTable;

        public LabelWidget m_furnaceLabel;

        public InventorySlotWidget m_catSlot;

        public InventorySlotWidget m_resultSlot;

        public InventorySlotWidget m_remainSlot;

        public FireWidget m_fire;

        public ValueBarWidget m_progress;

        public CokeOvenWidget(IInventory inventory, ComponentCokeOven componentCraftingTable) {
            m_componentCraftingTable = componentCraftingTable;
            XElement node = ContentManager.Get<XElement>("Widgets/CokeOvenWidget");
            LoadContents(this, node);
            m_inventoryGrid = Children.Find<GridPanelWidget>("InventoryGrid");
            m_metalGrid = Children.Find<GridPanelWidget>("FurnaceGrid");
            m_catSlot = Children.Find<InventorySlotWidget>("CatSlot");
            m_furnaceLabel = Children.Find<LabelWidget>("FurnaceLabel");
            m_fire = Children.Find<FireWidget>("Fire");
            m_progress = Children.Find<ValueBarWidget>("Progress");
            m_remainSlot = Children.Find<InventorySlotWidget>("RemainsSlot");
            m_resultSlot = Children.Find<InventorySlotWidget>("ResultSlot");
            int num = 10;
            for (int i = 0; i < m_inventoryGrid.RowsCount; i++) {
                for (int j = 0; j < m_inventoryGrid.ColumnsCount; j++) {
                    var inventorySlotWidget = new InventorySlotWidget();
                    inventorySlotWidget.AssignInventorySlot(inventory, num++);
                    m_inventoryGrid.Children.Add(inventorySlotWidget);
                    m_inventoryGrid.SetWidgetCell(inventorySlotWidget, new Point2(j, i));
                }
            }
            num = 0;
            for (int i = 0; i < m_metalGrid.RowsCount; i++) {
                for (int j = 0; j < m_metalGrid.ColumnsCount; j++) {
                    var inventorySlotWidget = new InventorySlotWidget();
                    inventorySlotWidget.AssignInventorySlot(componentCraftingTable, num++);
                    m_metalGrid.Children.Add(inventorySlotWidget);
                    m_metalGrid.SetWidgetCell(inventorySlotWidget, new Point2(j, i));
                }
            }

            m_resultSlot.AssignInventorySlot(componentCraftingTable, componentCraftingTable.ResultSlotIndex);
            m_remainSlot.AssignInventorySlot(componentCraftingTable, componentCraftingTable.RemainsSlotIndex);
            m_catSlot.AssignInventorySlot(componentCraftingTable, componentCraftingTable.CatSlotIndex);
        }

        public override void Update() {
            m_furnaceLabel.Text = "焦炉" + (m_componentCraftingTable.IsIntegrity ? " (已成型)" : "");
            m_fire.ParticlesPerSecond = m_componentCraftingTable.IsWorking ? 24 : 0;
            m_progress.Value = m_componentCraftingTable.m_craftingProgress;
            if (!m_componentCraftingTable.IsAddedToProject) {
                ParentWidget.Children.Remove(this);
            }
        }
    }
}